package editor;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedList;

import javax.swing.*;

public class TilesetDialog extends JDialog implements ActionListener
{
	private JButton newTile;
	private JButton loadTile;
	private JLabel lblName;
	private JTextField name;
	private JButton finName;
	private JTextArea tiles;
	private JButton done;
	private JButton cancel;
	
	private Tileset edited;
	
	public TilesetDialog( JFrame parent )
	{
		super( parent, true );
		
		this.setSize( 400, 400 );

		addUI();
		
		this.setVisible( true );
	}
	
	public TilesetDialog( JFrame parent, Tileset ts )
	{
		super( parent, true );
		
		this.setSize( 400, 400 );
		
		addUI();
		
		finName.setEnabled( false );
		
		loadTile.setEnabled( true );
		newTile.setEnabled( true );
		
		name.setText( ts.getName() );
		name.setEditable( false );
		
		String s = "";
		for( String str : ts.getListTiles() )
		{
			s += str + "\n";
		}
		
		tiles.setText( s );
		
		edited = ts;
		
		this.setVisible( true );
	}
	
	public void addUI()
	{
		newTile = new JButton( "New Tile" );
		loadTile = new JButton( "Load Tile" );
		done = new JButton( "Done" );
		cancel = new JButton( "Cancel" );
		
		lblName = new JLabel( "Tileset Name:" );
		name = new JTextField( "Enter Tileset Name" );
		finName = new JButton( "Finalize Name" );
		
		tiles = new JTextArea();
		tiles.setEditable( false );
		tiles.setPreferredSize( new Dimension( 200, 200 ) );
		
		newTile.setEnabled( false );
		loadTile.setEnabled( false );

		GridBagLayout layout = new GridBagLayout();
		addComponent( lblName, 	1, 1, 1, 1, layout );
		addComponent( name, 	2, 1, 1, 1, layout );
		addComponent( finName, 	3, 1, 1, 1, layout );
		addComponent( newTile, 	1, 2, 1, 1, layout );
		addComponent( loadTile, 3, 2, 1, 1, layout );
		addComponent( tiles, 	1, 3, 3, 1, layout );
		addComponent( done, 	1, 4, 1, 1, layout );
		addComponent( cancel, 	3, 4, 1, 1, layout );
		
		newTile.addActionListener( this );
		loadTile.addActionListener( this );
		finName.addActionListener( this );
		done.addActionListener( this );
		cancel.addActionListener( this );
		
		this.setLayout( layout );
	}
	
	private void addComponent( Component c, int col, int row, int width, int height, GridBagLayout layout )
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = col;
		gbc.gridy = row;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = 100;
		gbc.weighty = 100;
		layout.setConstraints( c, gbc );
		gbc.insets = new Insets( 5, 5, 5, 5 );
		this.add( c );
	}
	
	public void actionPerformed( ActionEvent e ) 
	{
		Object o = e.getSource();

		if( o.equals( newTile ) )
		{
			TileDialog td = new TileDialog( (JFrame)this.getParent(), name.getText() );
			String file = td.getTileFile();
			
			if( file != null )
			{
				tiles.setText( tiles.getText() + file + "\n" );
			
				if( edited != null )
				{
					edited.getListTiles().add( file );
					edited.getTiles().add( Tile.load( file ) );
				}
			}
		}
		else if( o.equals( loadTile ) )
		{
			String file = Tile.load( this ).getImage().getFilename();
			
			tiles.setText( tiles.getText() + file + "\n" );
			
			if( edited != null )
			{
				edited.getListTiles().add( file );
				edited.getTiles().add( Tile.load( file ) );
			}
		}
		else if( o.equals( finName ) )
		{
			newTile.setEnabled( true );
			loadTile.setEnabled( true );
			name.setEditable( false );
		}
		else if( o.equals( done ) )
		{
			LinkedList<String> str = new LinkedList<String>();
	        
			BufferedReader reader = new BufferedReader( new StringReader( tiles.getText() ) );
			        
			try 
			{
				String s;
				while ( (s = reader.readLine() ) != null )
				{
				  if ( s.length() > 0 )
					  str.add( s );
				}
			} 
			catch( IOException err ) 
			{
			  err.printStackTrace();
			}
			
			(new Tileset( name.getText(), str )).save();
			this.dispose();
		}
		else if( o.equals( cancel ) )
		{
			this.dispose();
		}
	}
	
	public Tileset getEdited()
	{
		return edited;
	}
}
